#pragma once

#include "AboutForm.h"
#include "GotoPacket.h"
#include "PidList.h"
#include <stdlib.h>

#include <stdio.h>
#include <string>
#include <io.h>
#include <share.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>

FILE *ip_file;
char *ip_filepath = "";
int end_of_file = 0;					// check for end of file
__int64 ip_file_length = 0;				// how long?
__int64 current_ts_packet = -1;
__int64 packetsNumb = 0;
int hexModeSwitch = 1;
int firstPacketOffset = 0;

namespace StreamEye {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;
	using namespace System::Runtime::InteropServices;

	/// <summary>
	/// Summary for MainForm
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class MainForm : public System::Windows::Forms::Form
	{
	public:
		MainForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MainForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	protected: 
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  helpToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::ToolStripMenuItem^  openToolStripMenuItem;
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::GroupBox^  groupBox2;
	private: System::Windows::Forms::ToolStripMenuItem^  closeToolStripMenuItem;
	private: System::Windows::Forms::ToolStrip^  toolStrip1;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton1;
	private: System::Windows::Forms::ToolStripSeparator^  toolStripSeparator1;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton2;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton3;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton4;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton5;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton6;
	private: System::Windows::Forms::ToolStripSeparator^  toolStripSeparator2;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton7;

	private: System::Windows::Forms::GroupBox^  groupBox4;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::TextBox^  textBox7;
	private: System::Windows::Forms::TextBox^  textBox8;
	private: System::Windows::Forms::TextBox^  textBox9;
	private: System::Windows::Forms::TextBox^  textBox10;
	private: System::Windows::Forms::TextBox^  textBox11;
	private: System::Windows::Forms::TextBox^  textBox12;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::GroupBox^  groupBox3;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::Label^  label9;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::GroupBox^  groupBox5;
	private: System::Windows::Forms::TextBox^  textBox13;
	private: System::Windows::Forms::Label^  label11;
	private: System::Windows::Forms::TextBox^  textBox14;
	private: System::Windows::Forms::TextBox^  textBox16;
	private: System::Windows::Forms::TextBox^  textBox15;
	private: System::Windows::Forms::TextBox^  textBox17;


	private: System::Windows::Forms::CheckBox^  checkBox2;
	private: System::Windows::Forms::CheckBox^  checkBox1;
	private: System::Windows::Forms::TextBox^  textBox19;
	private: System::Windows::Forms::TextBox^  textBox18;
	private: System::Windows::Forms::ProgressBar^  progressBar1;
	private: System::Windows::Forms::ToolStripMenuItem^  toolsToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  hexToolStripMenuItem;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton8;
	private: System::Windows::Forms::ToolStripButton^  toolStripButton9;




	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainForm::typeid));
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->toolsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->hexToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox16 = (gcnew System::Windows::Forms::TextBox());
			this->textBox15 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->toolStrip1 = (gcnew System::Windows::Forms::ToolStrip());
			this->toolStripButton1 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripSeparator1 = (gcnew System::Windows::Forms::ToolStripSeparator());
			this->toolStripButton2 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton3 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton4 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton5 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton6 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripSeparator2 = (gcnew System::Windows::Forms::ToolStripSeparator());
			this->toolStripButton8 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton9 = (gcnew System::Windows::Forms::ToolStripButton());
			this->toolStripButton7 = (gcnew System::Windows::Forms::ToolStripButton());
			this->groupBox4 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox19 = (gcnew System::Windows::Forms::TextBox());
			this->textBox18 = (gcnew System::Windows::Forms::TextBox());
			this->checkBox2 = (gcnew System::Windows::Forms::CheckBox());
			this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->textBox7 = (gcnew System::Windows::Forms::TextBox());
			this->textBox8 = (gcnew System::Windows::Forms::TextBox());
			this->textBox9 = (gcnew System::Windows::Forms::TextBox());
			this->textBox10 = (gcnew System::Windows::Forms::TextBox());
			this->textBox11 = (gcnew System::Windows::Forms::TextBox());
			this->textBox12 = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->groupBox5 = (gcnew System::Windows::Forms::GroupBox());
			this->label11 = (gcnew System::Windows::Forms::Label());
			this->textBox14 = (gcnew System::Windows::Forms::TextBox());
			this->textBox13 = (gcnew System::Windows::Forms::TextBox());
			this->textBox17 = (gcnew System::Windows::Forms::TextBox());
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
			this->menuStrip1->SuspendLayout();
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->toolStrip1->SuspendLayout();
			this->groupBox4->SuspendLayout();
			this->groupBox3->SuspendLayout();
			this->groupBox5->SuspendLayout();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->fileToolStripMenuItem, 
				this->toolsToolStripMenuItem, this->helpToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(913, 24);
			this->menuStrip1->TabIndex = 0;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->openToolStripMenuItem, 
				this->closeToolStripMenuItem, this->exitToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
			this->fileToolStripMenuItem->Text = L"File";
			// 
			// openToolStripMenuItem
			// 
			this->openToolStripMenuItem->Name = L"openToolStripMenuItem";
			this->openToolStripMenuItem->Size = System::Drawing::Size(205, 22);
			this->openToolStripMenuItem->Text = L"Open transport stream ...";
			this->openToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::openToolStripMenuItem_Click);
			// 
			// closeToolStripMenuItem
			// 
			this->closeToolStripMenuItem->Enabled = false;
			this->closeToolStripMenuItem->Name = L"closeToolStripMenuItem";
			this->closeToolStripMenuItem->Size = System::Drawing::Size(205, 22);
			this->closeToolStripMenuItem->Text = L"Close";
			this->closeToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::closeToolStripMenuItem_Click);
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(205, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::exitToolStripMenuItem_Click);
			// 
			// toolsToolStripMenuItem
			// 
			this->toolsToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->hexToolStripMenuItem});
			this->toolsToolStripMenuItem->Name = L"toolsToolStripMenuItem";
			this->toolsToolStripMenuItem->Size = System::Drawing::Size(48, 20);
			this->toolsToolStripMenuItem->Text = L"Tools";
			// 
			// hexToolStripMenuItem
			// 
			this->hexToolStripMenuItem->Checked = true;
			this->hexToolStripMenuItem->CheckOnClick = true;
			this->hexToolStripMenuItem->CheckState = System::Windows::Forms::CheckState::Checked;
			this->hexToolStripMenuItem->Enabled = false;
			this->hexToolStripMenuItem->Name = L"hexToolStripMenuItem";
			this->hexToolStripMenuItem->Size = System::Drawing::Size(94, 22);
			this->hexToolStripMenuItem->Text = L"Hex";
			this->hexToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::hexToolStripMenuItem_Click);
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->aboutToolStripMenuItem});
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(44, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			this->aboutToolStripMenuItem->Size = System::Drawing::Size(107, 22);
			this->aboutToolStripMenuItem->Text = L"About";
			this->aboutToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::aboutToolStripMenuItem_Click);
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->textBox2);
			this->groupBox1->Controls->Add(this->textBox1);
			this->groupBox1->Controls->Add(this->label2);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Location = System::Drawing::Point(266, 49);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(406, 69);
			this->groupBox1->TabIndex = 1;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"TS File";
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(48, 43);
			this->textBox2->Name = L"textBox2";
			this->textBox2->ReadOnly = true;
			this->textBox2->Size = System::Drawing::Size(337, 20);
			this->textBox2->TabIndex = 2;
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(48, 18);
			this->textBox1->Name = L"textBox1";
			this->textBox1->ReadOnly = true;
			this->textBox1->Size = System::Drawing::Size(337, 20);
			this->textBox1->TabIndex = 2;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(17, 46);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(25, 13);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Info";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(13, 21);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(29, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Path";
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->Filter = L"Transport streams|*.ts;*.trp;*.m2t;*.m2ts";
			// 
			// textBox3
			// 
			this->textBox3->BackColor = System::Drawing::SystemColors::ButtonHighlight;
			this->textBox3->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox3->Location = System::Drawing::Point(51, 19);
			this->textBox3->Multiline = true;
			this->textBox3->Name = L"textBox3";
			this->textBox3->ReadOnly = true;
			this->textBox3->Size = System::Drawing::Size(337, 177);
			this->textBox3->TabIndex = 2;
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->textBox16);
			this->groupBox2->Controls->Add(this->textBox15);
			this->groupBox2->Controls->Add(this->textBox4);
			this->groupBox2->Controls->Add(this->textBox3);
			this->groupBox2->Location = System::Drawing::Point(266, 135);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(406, 273);
			this->groupBox2->TabIndex = 3;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"TS packet";
			// 
			// textBox16
			// 
			this->textBox16->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox16->Location = System::Drawing::Point(19, 243);
			this->textBox16->Name = L"textBox16";
			this->textBox16->ReadOnly = true;
			this->textBox16->Size = System::Drawing::Size(369, 20);
			this->textBox16->TabIndex = 10;
			// 
			// textBox15
			// 
			this->textBox15->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox15->Location = System::Drawing::Point(19, 222);
			this->textBox15->Name = L"textBox15";
			this->textBox15->ReadOnly = true;
			this->textBox15->Size = System::Drawing::Size(369, 20);
			this->textBox15->TabIndex = 9;
			// 
			// textBox4
			// 
			this->textBox4->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox4->Location = System::Drawing::Point(20, 19);
			this->textBox4->Multiline = true;
			this->textBox4->Name = L"textBox4";
			this->textBox4->ReadOnly = true;
			this->textBox4->Size = System::Drawing::Size(25, 177);
			this->textBox4->TabIndex = 3;
			this->textBox4->Text = L"00 10 20 30 40 50 60 70 80 90 A0 B0";
			// 
			// toolStrip1
			// 
			this->toolStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(11) {this->toolStripButton1, 
				this->toolStripSeparator1, this->toolStripButton2, this->toolStripButton3, this->toolStripButton4, this->toolStripButton5, this->toolStripButton6, 
				this->toolStripSeparator2, this->toolStripButton8, this->toolStripButton9, this->toolStripButton7});
			this->toolStrip1->Location = System::Drawing::Point(0, 24);
			this->toolStrip1->Name = L"toolStrip1";
			this->toolStrip1->Size = System::Drawing::Size(913, 25);
			this->toolStrip1->TabIndex = 4;
			this->toolStrip1->Text = L"toolStrip1";
			// 
			// toolStripButton1
			// 
			this->toolStripButton1->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton1.Image")));
			this->toolStripButton1->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton1->Name = L"toolStripButton1";
			this->toolStripButton1->Size = System::Drawing::Size(23, 22);
			this->toolStripButton1->Text = L"Open TS file ...";
			this->toolStripButton1->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton1_Click);
			// 
			// toolStripSeparator1
			// 
			this->toolStripSeparator1->Name = L"toolStripSeparator1";
			this->toolStripSeparator1->Size = System::Drawing::Size(6, 25);
			// 
			// toolStripButton2
			// 
			this->toolStripButton2->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton2->Enabled = false;
			this->toolStripButton2->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton2.Image")));
			this->toolStripButton2->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton2->Name = L"toolStripButton2";
			this->toolStripButton2->Size = System::Drawing::Size(23, 22);
			this->toolStripButton2->Text = L"Goto first packet";
			this->toolStripButton2->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton2_Click);
			// 
			// toolStripButton3
			// 
			this->toolStripButton3->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton3->Enabled = false;
			this->toolStripButton3->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton3.Image")));
			this->toolStripButton3->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton3->Name = L"toolStripButton3";
			this->toolStripButton3->Size = System::Drawing::Size(23, 22);
			this->toolStripButton3->Text = L"Goto previous packet";
			this->toolStripButton3->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton3_Click);
			// 
			// toolStripButton4
			// 
			this->toolStripButton4->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton4->Enabled = false;
			this->toolStripButton4->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton4.Image")));
			this->toolStripButton4->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton4->Name = L"toolStripButton4";
			this->toolStripButton4->Size = System::Drawing::Size(23, 22);
			this->toolStripButton4->Text = L"Goto next packet";
			this->toolStripButton4->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton4_Click);
			// 
			// toolStripButton5
			// 
			this->toolStripButton5->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton5->Enabled = false;
			this->toolStripButton5->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton5.Image")));
			this->toolStripButton5->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton5->Name = L"toolStripButton5";
			this->toolStripButton5->Size = System::Drawing::Size(23, 22);
			this->toolStripButton5->Text = L"Goto last packet";
			this->toolStripButton5->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton5_Click);
			// 
			// toolStripButton6
			// 
			this->toolStripButton6->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton6->Enabled = false;
			this->toolStripButton6->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton6.Image")));
			this->toolStripButton6->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton6->Name = L"toolStripButton6";
			this->toolStripButton6->Size = System::Drawing::Size(23, 22);
			this->toolStripButton6->Text = L"Goto packet number ...";
			this->toolStripButton6->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton6_Click);
			// 
			// toolStripSeparator2
			// 
			this->toolStripSeparator2->Name = L"toolStripSeparator2";
			this->toolStripSeparator2->Size = System::Drawing::Size(6, 25);
			// 
			// toolStripButton8
			// 
			this->toolStripButton8->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton8->Enabled = false;
			this->toolStripButton8->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton8.Image")));
			this->toolStripButton8->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton8->Name = L"toolStripButton8";
			this->toolStripButton8->Size = System::Drawing::Size(23, 22);
			this->toolStripButton8->Text = L"PID List";
			this->toolStripButton8->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton8_Click);
			// 
			// toolStripButton9
			// 
			this->toolStripButton9->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton9->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton9.Image")));
			this->toolStripButton9->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton9->Name = L"toolStripButton9";
			this->toolStripButton9->Size = System::Drawing::Size(23, 22);
			this->toolStripButton9->Text = L"Separate MPTS";
			this->toolStripButton9->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton9_Click);
			// 
			// toolStripButton7
			// 
			this->toolStripButton7->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
			this->toolStripButton7->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"toolStripButton7.Image")));
			this->toolStripButton7->ImageTransparentColor = System::Drawing::Color::Magenta;
			this->toolStripButton7->Name = L"toolStripButton7";
			this->toolStripButton7->Size = System::Drawing::Size(23, 22);
			this->toolStripButton7->Text = L"About";
			this->toolStripButton7->Click += gcnew System::EventHandler(this, &MainForm::toolStripButton7_Click);
			// 
			// groupBox4
			// 
			this->groupBox4->Controls->Add(this->textBox19);
			this->groupBox4->Controls->Add(this->textBox18);
			this->groupBox4->Controls->Add(this->checkBox2);
			this->groupBox4->Controls->Add(this->checkBox1);
			this->groupBox4->Location = System::Drawing::Point(13, 49);
			this->groupBox4->Name = L"groupBox4";
			this->groupBox4->Size = System::Drawing::Size(247, 69);
			this->groupBox4->TabIndex = 6;
			this->groupBox4->TabStop = false;
			this->groupBox4->Text = L"Filter";
			// 
			// textBox19
			// 
			this->textBox19->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox19->Location = System::Drawing::Point(174, 39);
			this->textBox19->Name = L"textBox19";
			this->textBox19->ReadOnly = true;
			this->textBox19->Size = System::Drawing::Size(50, 20);
			this->textBox19->TabIndex = 9;
			// 
			// textBox18
			// 
			this->textBox18->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox18->Location = System::Drawing::Point(174, 16);
			this->textBox18->Name = L"textBox18";
			this->textBox18->ReadOnly = true;
			this->textBox18->Size = System::Drawing::Size(50, 20);
			this->textBox18->TabIndex = 8;
			// 
			// checkBox2
			// 
			this->checkBox2->AutoSize = true;
			this->checkBox2->Enabled = false;
			this->checkBox2->Location = System::Drawing::Point(21, 41);
			this->checkBox2->Name = L"checkBox2";
			this->checkBox2->Size = System::Drawing::Size(44, 17);
			this->checkBox2->TabIndex = 1;
			this->checkBox2->Text = L"PID";
			this->checkBox2->UseVisualStyleBackColor = true;
			this->checkBox2->CheckedChanged += gcnew System::EventHandler(this, &MainForm::checkBox2_CheckedChanged);
			// 
			// checkBox1
			// 
			this->checkBox1->AutoSize = true;
			this->checkBox1->Enabled = false;
			this->checkBox1->Location = System::Drawing::Point(21, 18);
			this->checkBox1->Name = L"checkBox1";
			this->checkBox1->Size = System::Drawing::Size(130, 17);
			this->checkBox1->TabIndex = 0;
			this->checkBox1->Text = L"Payload start indicator";
			this->checkBox1->UseVisualStyleBackColor = true;
			// 
			// textBox5
			// 
			this->textBox5->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox5->Location = System::Drawing::Point(191, 19);
			this->textBox5->Name = L"textBox5";
			this->textBox5->ReadOnly = true;
			this->textBox5->Size = System::Drawing::Size(50, 20);
			this->textBox5->TabIndex = 0;
			// 
			// textBox6
			// 
			this->textBox6->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox6->Location = System::Drawing::Point(191, 39);
			this->textBox6->Name = L"textBox6";
			this->textBox6->ReadOnly = true;
			this->textBox6->Size = System::Drawing::Size(50, 20);
			this->textBox6->TabIndex = 1;
			// 
			// textBox7
			// 
			this->textBox7->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox7->Location = System::Drawing::Point(191, 59);
			this->textBox7->Name = L"textBox7";
			this->textBox7->ReadOnly = true;
			this->textBox7->Size = System::Drawing::Size(50, 20);
			this->textBox7->TabIndex = 2;
			// 
			// textBox8
			// 
			this->textBox8->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox8->Location = System::Drawing::Point(191, 79);
			this->textBox8->Name = L"textBox8";
			this->textBox8->ReadOnly = true;
			this->textBox8->Size = System::Drawing::Size(50, 20);
			this->textBox8->TabIndex = 3;
			// 
			// textBox9
			// 
			this->textBox9->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox9->Location = System::Drawing::Point(191, 99);
			this->textBox9->Name = L"textBox9";
			this->textBox9->ReadOnly = true;
			this->textBox9->Size = System::Drawing::Size(50, 20);
			this->textBox9->TabIndex = 4;
			// 
			// textBox10
			// 
			this->textBox10->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox10->Location = System::Drawing::Point(191, 119);
			this->textBox10->Name = L"textBox10";
			this->textBox10->ReadOnly = true;
			this->textBox10->Size = System::Drawing::Size(50, 20);
			this->textBox10->TabIndex = 5;
			// 
			// textBox11
			// 
			this->textBox11->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox11->Location = System::Drawing::Point(191, 139);
			this->textBox11->Name = L"textBox11";
			this->textBox11->ReadOnly = true;
			this->textBox11->Size = System::Drawing::Size(50, 20);
			this->textBox11->TabIndex = 6;
			// 
			// textBox12
			// 
			this->textBox12->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox12->Location = System::Drawing::Point(191, 159);
			this->textBox12->Name = L"textBox12";
			this->textBox12->ReadOnly = true;
			this->textBox12->Size = System::Drawing::Size(50, 20);
			this->textBox12->TabIndex = 7;
			// 
			// label3
			// 
			this->label3->Location = System::Drawing::Point(6, 21);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(179, 14);
			this->label3->TabIndex = 8;
			this->label3->Text = L"Sync byte";
			this->label3->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label4
			// 
			this->label4->Location = System::Drawing::Point(6, 41);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(179, 14);
			this->label4->TabIndex = 9;
			this->label4->Text = L"Transport error indicator";
			this->label4->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label5
			// 
			this->label5->Location = System::Drawing::Point(6, 61);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(179, 14);
			this->label5->TabIndex = 10;
			this->label5->Text = L"Payload unit start indicator";
			this->label5->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label6
			// 
			this->label6->Location = System::Drawing::Point(6, 81);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(179, 14);
			this->label6->TabIndex = 11;
			this->label6->Text = L"Transport priority";
			this->label6->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label7
			// 
			this->label7->Location = System::Drawing::Point(6, 101);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(179, 14);
			this->label7->TabIndex = 12;
			this->label7->Text = L"Program identification number";
			this->label7->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// groupBox3
			// 
			this->groupBox3->Controls->Add(this->label10);
			this->groupBox3->Controls->Add(this->label9);
			this->groupBox3->Controls->Add(this->label8);
			this->groupBox3->Controls->Add(this->label7);
			this->groupBox3->Controls->Add(this->label6);
			this->groupBox3->Controls->Add(this->label5);
			this->groupBox3->Controls->Add(this->label4);
			this->groupBox3->Controls->Add(this->label3);
			this->groupBox3->Controls->Add(this->textBox12);
			this->groupBox3->Controls->Add(this->textBox11);
			this->groupBox3->Controls->Add(this->textBox10);
			this->groupBox3->Controls->Add(this->textBox9);
			this->groupBox3->Controls->Add(this->textBox8);
			this->groupBox3->Controls->Add(this->textBox7);
			this->groupBox3->Controls->Add(this->textBox6);
			this->groupBox3->Controls->Add(this->textBox5);
			this->groupBox3->Location = System::Drawing::Point(13, 135);
			this->groupBox3->Name = L"groupBox3";
			this->groupBox3->Size = System::Drawing::Size(247, 216);
			this->groupBox3->TabIndex = 5;
			this->groupBox3->TabStop = false;
			this->groupBox3->Text = L"TS header";
			// 
			// label10
			// 
			this->label10->Location = System::Drawing::Point(6, 161);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(179, 14);
			this->label10->TabIndex = 15;
			this->label10->Text = L"Continuity counter";
			this->label10->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label9
			// 
			this->label9->Location = System::Drawing::Point(6, 141);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(179, 14);
			this->label9->TabIndex = 14;
			this->label9->Text = L"Adaptation field control";
			this->label9->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// label8
			// 
			this->label8->Location = System::Drawing::Point(6, 121);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(179, 14);
			this->label8->TabIndex = 13;
			this->label8->Text = L"Transport scrambling control";
			this->label8->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// groupBox5
			// 
			this->groupBox5->Controls->Add(this->label11);
			this->groupBox5->Controls->Add(this->textBox14);
			this->groupBox5->Controls->Add(this->textBox13);
			this->groupBox5->Location = System::Drawing::Point(13, 357);
			this->groupBox5->Name = L"groupBox5";
			this->groupBox5->Size = System::Drawing::Size(247, 70);
			this->groupBox5->TabIndex = 7;
			this->groupBox5->TabStop = false;
			this->groupBox5->Text = L"Adaptation";
			// 
			// label11
			// 
			this->label11->Location = System::Drawing::Point(6, 21);
			this->label11->Name = L"label11";
			this->label11->Size = System::Drawing::Size(179, 14);
			this->label11->TabIndex = 16;
			this->label11->Text = L"Adaptation byte count";
			this->label11->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
			// 
			// textBox14
			// 
			this->textBox14->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox14->Location = System::Drawing::Point(9, 40);
			this->textBox14->Name = L"textBox14";
			this->textBox14->ReadOnly = true;
			this->textBox14->Size = System::Drawing::Size(232, 20);
			this->textBox14->TabIndex = 9;
			// 
			// textBox13
			// 
			this->textBox13->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox13->Location = System::Drawing::Point(191, 19);
			this->textBox13->Name = L"textBox13";
			this->textBox13->ReadOnly = true;
			this->textBox13->Size = System::Drawing::Size(50, 20);
			this->textBox13->TabIndex = 8;
			// 
			// textBox17
			// 
			this->textBox17->BackColor = System::Drawing::SystemColors::ButtonHighlight;
			this->textBox17->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->textBox17->Location = System::Drawing::Point(678, 53);
			this->textBox17->Multiline = true;
			this->textBox17->Name = L"textBox17";
			this->textBox17->ReadOnly = true;
			this->textBox17->Size = System::Drawing::Size(223, 355);
			this->textBox17->TabIndex = 8;
			// 
			// progressBar1
			// 
			this->progressBar1->Location = System::Drawing::Point(13, 436);
			this->progressBar1->Maximum = 10000;
			this->progressBar1->Name = L"progressBar1";
			this->progressBar1->Size = System::Drawing::Size(888, 16);
			this->progressBar1->Step = 1;
			this->progressBar1->TabIndex = 9;
			// 
			// MainForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(913, 464);
			this->Controls->Add(this->progressBar1);
			this->Controls->Add(this->textBox17);
			this->Controls->Add(this->groupBox5);
			this->Controls->Add(this->groupBox4);
			this->Controls->Add(this->groupBox3);
			this->Controls->Add(this->toolStrip1);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"MainForm";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MPEG-2 TS packet analyser";
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			this->toolStrip1->ResumeLayout(false);
			this->toolStrip1->PerformLayout();
			this->groupBox4->ResumeLayout(false);
			this->groupBox4->PerformLayout();
			this->groupBox3->ResumeLayout(false);
			this->groupBox3->PerformLayout();
			this->groupBox5->ResumeLayout(false);
			this->groupBox5->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

// Control Methods
	private: System::Void openFile(){
				 if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ){
					 closeToolStripMenuItem->Enabled = true;
					 toolStripButton2->Enabled = true;
					 toolStripButton3->Enabled = true;
					 toolStripButton4->Enabled = true;
					 toolStripButton5->Enabled = true;
					 toolStripButton6->Enabled = true;
					 hexToolStripMenuItem->Enabled = true;
					 checkBox2->Enabled = true;
					 toolStripButton8->Enabled = true;

					 reset();

					 textBox1->Text = openFileDialog1->FileName;
					 textBox2->Text = GetFileInfo(openFileDialog1->FileName);					 

					 ip_filepath = (char*)(void*)Marshal::StringToHGlobalAnsi(openFileDialog1->FileName);
					 fopen_s(&ip_file, ip_filepath, "rb");					 
					 ip_file_length = get_ip_file_length(ip_filepath);
					 GetPacketStart();
					 GetPacket(1);
				 }
			 }
	private: System::Void closeFile(){
				 closeToolStripMenuItem->Enabled = false;
				 toolStripButton2->Enabled = false;
				 toolStripButton3->Enabled = false;
				 toolStripButton4->Enabled = false;
				 toolStripButton5->Enabled = false;
				 toolStripButton6->Enabled = false;
				 hexToolStripMenuItem->Enabled = false;
				 checkBox2->Enabled = false;
				 toolStripButton8->Enabled = false;

				 reset();
			 }
	private: System::Void exitProg(){
				Application::Exit();
			 }	

// Menu
	private: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 openFile();
			 }	
	private: System::Void closeToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 closeFile();
			 }
	private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 exitProg();
			 }
	private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 openAbtForm();
			 }
	private: System::Void checkBox2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
				 if (checkBox2->Checked != true) {
					 textBox19->ReadOnly = true;
					 textBox19->Text = "";
				 }
				 else
				 {
					 textBox19->ReadOnly = false;
					 textBox19->Text = textBox9->Text;
				 }
			 }
	private: System::Void hexToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (hexToolStripMenuItem->Checked == true)
					 hexMode(true);
				 else
					 hexMode(false);
			 }
// Tool panel
	private: System::Void toolStripButton1_Click(System::Object^  sender, System::EventArgs^  e) {
				 openFile();
			 }
	private: System::Void toolStripButton2_Click(System::Object^  sender, System::EventArgs^  e) {
				 gotoFirstPacket();
			 }
	private: System::Void toolStripButton4_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (textBox19->Text == "")
					 nextPacket();
				 else 
					 goUpToPid(textBox19->Text);
			 }
	private: System::Void toolStripButton3_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (textBox19->Text == "")
					 previousPacket();
				 else 
					 goDownToPid(textBox19->Text);
			 }
	private: System::Void toolStripButton5_Click(System::Object^  sender, System::EventArgs^  e) {
				 gotoLastPacket();
			 }
	private: System::Void toolStripButton6_Click(System::Object^  sender, System::EventArgs^  e) {
				 gotoPacketNumber();
			 }
	private: System::Void toolStripButton7_Click(System::Object^  sender, System::EventArgs^  e) {
				 openAbtForm();
			 }
	private: System::Void toolStripButton8_Click(System::Object^  sender, System::EventArgs^  e) {
				 listAllPids();
			 }
	private: System::Void toolStripButton9_Click(System::Object^  sender, System::EventArgs^  e) {
				 separator();
			
			 }

// Auxiliary
	private: System::String^ GetFileInfo(System::String^ file) {
				 FileInfo^ f = gcnew FileInfo( file );				 
				 String^ fileSize = Decimal::Round((f->Length) / 1024).ToString();

				 packetsNumb = (f->Length) / 188;
				 String^ packetsNum = Decimal::Round((f->Length) / 188).ToString();
				 String^ fileInfo = fileSize + "Kb, " + packetsNum + " 188-byte packets";
				 return fileInfo;
			 }

	private: int GetPacketStart() {
				 int packetStart = 0;//debug

				 int sync_data = 0;				 
				 _fseeki64(ip_file, 0, SEEK_SET);
				 for (int i = 0; i<=187; i++) {
					 packetStart = ftell(ip_file);//debug
					 fread(&sync_data, 1, 1, ip_file);
					 if (sync_data == 71){
						 firstPacketOffset = i;
						 break;
					 }
				 }
				 return firstPacketOffset;
			}

	private: void GetPacket(__int64 packetNum) {
				 current_ts_packet = packetNum;
				 String ^packet = "";
				 int sync_data = 0;			 

				 __int64 offset = (packetNum-1)*188+firstPacketOffset;
				 _fseeki64(ip_file, offset, SEEK_SET);
				 
				 for (int i = 1; i<=188; i++)
				 {
					 fread(&sync_data, 1, 1, ip_file);
					 packet += sync_data.ToString("X2");					 
					 if ((i%8 == 0) & (i%16 !=0))
						 packet += "|";
					 else 
						 packet += " ";
				 }				 
				 groupBox2->Text = "TS packet " + packetNum;
				 textBox3->Text = packet;
				 GetTSheader();				 
			 }
	private: void GetTSheader() {
				 if (hexModeSwitch == 1) 
					 GetTSheaderHex();				 
				 else
					 GetTSheaderDec();					 
			 }

	private: void GetTSheaderHex() {
				 array<String^>^ tsHeader = gcnew array<String^>(8);
				 
				 tsHeader[0]=binToHex(getBytes(1,8)); // Sync byte
				 tsHeader[1]=getBytes(9,9); // Transport error indicator
				 tsHeader[2]=getBytes(10,10); // Payload unit start indicator
				 tsHeader[3]=getBytes(11,11); // Transport priority
				 tsHeader[4]=binToHex(getBytes(12,24)); // Program identification number
				 tsHeader[5]=getBytes(25,26); // Transport scrambling control 
				 tsHeader[6]=getBytes(27,28); // Adaptation field control
				 tsHeader[7]=binToHex(getBytes(29,32)); // Continuity counter
				 updateHeader(tsHeader);				 
				 progressBar1->Value = Convert::ToInt16(10000*current_ts_packet/packetsNumb);
			 }

	private: void GetTSheaderDec() {				 
				 array<String^>^ tsHeader = gcnew array<String^>(8);
				 
				 tsHeader[0]=binToDec(getBytes(1,8)); // Sync byte
				 tsHeader[1]=getBytes(9,9); // Transport error indicator
				 tsHeader[2]=getBytes(10,10); // Payload unit start indicator
				 tsHeader[3]=getBytes(11,11); // Transport priority
				 tsHeader[4]=binToDec(getBytes(12,24)); // Program identification number
				 tsHeader[5]=getBytes(25,26); // Transport scrambling control 
				 tsHeader[6]=getBytes(27,28); // Adaptation field control
				 tsHeader[7]=binToDec(getBytes(29,32)); // Continuity counter
				 updateHeader(tsHeader);
				 progressBar1->Value = Convert::ToInt16(10000*current_ts_packet/packetsNumb);
			 }
	
	private: void updateHeader(array<String^>^ tsHeader) {				 
				 textBox5->Text = tsHeader[0];
				 textBox6->Text = tsHeader[1];
				 textBox7->Text = tsHeader[2];
				 textBox8->Text = tsHeader[3];
				 textBox9->Text = tsHeader[4];
				 textBox10->Text = tsHeader[5];
				 textBox11->Text = tsHeader[6];
				 textBox12->Text = tsHeader[7];
			 }

	private: System::String ^getBytes (int beginBit, int endBit) {
				 String ^bytes = "";
				 int sync_data = 0;

				 //String ^bytesH = "";

				 __int64 offset = (current_ts_packet-1)*188+firstPacketOffset + (beginBit-1)/8;
				 _fseeki64(ip_file, offset, SEEK_SET);
				 //packetStart = ftell(ip_file);

				 for (int i = ((beginBit-1)/8+1); i<=((endBit-1)/8+1); i++)
				 {
					 fread(&sync_data, 1, 1, ip_file);					 
					 bytes += Convert::ToString(sync_data,2)->PadLeft(8,'0');					 
				 }
				 String ^bin = bytes->Substring((beginBit-1)%8, (1+endBit-beginBit));
				 return bin;
			 }

	private: String ^binToHex(String ^bin) {
				 // WORKS ONLY FOR bin->Length >= 4
				 int binlength;
				 String ^hex = "";
				 String ^binFour = "";
				 int binFourInt = 0;
				 
				 if (bin->Length%4 !=0)
					 binlength = 4*(bin->Length/4+1);
				 else
					 binlength = bin->Length;

				 String ^binFour16 = bin->PadLeft(binlength,'0');				 

				 for (int i=0; i < binFour16->Length/4; i++){
					 binFour = binFour16->Substring(i*4,4);
					 binFourInt = Convert::ToInt16(binFour);
	 				 switch (binFourInt) {
						 case 0: hex += "0"; break;
						 case 1: hex += "1"; break;
						 case 10: hex += "2"; break;
						 case 11: hex += "3"; break;
						 case 100: hex += "4"; break;
						 case 101: hex += "5"; break;
						 case 110: hex += "6"; break;
						 case 111: hex += "7"; break;
						 case 1000: hex += "8"; break;
						 case 1001: hex += "9"; break;
						 case 1010: hex += "A"; break;
						 case 1011: hex += "B"; break;
						 case 1100: hex += "C"; break;
						 case 1101: hex += "D"; break;
						 case 1110: hex += "E"; break;
						 case 1111: hex += "F"; break;
					 }
				 }
				 hex = "0x" + hex->PadLeft(4,'0');
				 return hex;
			 }

	private: String ^binToDec(String ^bin) {
				 String ^dec = "";
				 int decInt = 0;
				 
				 for (int j=bin->Length; j>= 1; j--) {
					 decInt += Convert::ToUInt16(bin->Substring(bin->Length-j,1))*Convert::ToUInt16(pow(2.0,j-1));
				 }
				 dec = decInt.ToString();
				 return dec;
			 }

	private: __int64 get_ip_file_length(char *ip_filepath)
			 {
				 //  find length of file
				 int ip_file2 = 0;
				 _sopen_s( &ip_file2, ip_filepath, _O_RDONLY, _SH_DENYWR, _S_IREAD );
				 if( ip_file2 == -1 )
				 {
					 Application::Exit(); //("Error: cannot open input file");
				 }
				 ip_file_length = _filelengthi64(ip_file2);
				 _close( ip_file2 );
				 return ip_file_length;
			 }
	private: void nextPacket()
			 {
				 if (current_ts_packet < packetsNumb)
				 {
					 current_ts_packet++;
					 GetPacket(current_ts_packet);
				 }
			 }
	private: void previousPacket()
			 {
				 if (current_ts_packet > 1)
				 {
					 current_ts_packet--;
					 GetPacket(current_ts_packet);
				 }
			 }
	private: void gotoFirstPacket() 
			 {
				 current_ts_packet = 1;
				 GetPacket(current_ts_packet);
			 }
	private: void gotoLastPacket() 
			 {
				 current_ts_packet = packetsNumb;
				 GetPacket(current_ts_packet);
			 }	
	private: void gotoPacketNumber()
			 {
				 String ^stringFromGotoPacketForm = "";
				 __int64 chosenPacket = 0;
				 GotoPacket^ testDialog = gcnew GotoPacket;
				 testDialog->setTextBox(current_ts_packet.ToString());
				 testDialog->setLabel(packetsNumb.ToString());
				 if ( testDialog->ShowDialog(this) == ::DialogResult::OK )
				 {				 
					 stringFromGotoPacketForm = testDialog->textBox1->Text;
					 chosenPacket = Convert::ToInt64(stringFromGotoPacketForm);
					 if (chosenPacket <= packetsNumb)
						 current_ts_packet = chosenPacket;
				 }
				 GetPacket(current_ts_packet);
			 }

	private: void goUpToPid(String ^pid){
				 __int64 startPacket = current_ts_packet;
				 for (__int64 i = startPacket+1; i <= packetsNumb; i++) {
					 current_ts_packet = i;
					 if (hexModeSwitch == 1)
					 {
						 if (binToHex(getBytes(12,24)) == pid) 
						 {
							 GetPacket(current_ts_packet);
							 break;
						 }
					 }
					 else 
					 {
						 if (binToDec(getBytes(12,24)) == pid) 
						 {
							 GetPacket(current_ts_packet);
							 break;
						 }
					 }
				 }
			 }
	private: void goDownToPid(String ^pid) {
				 __int64 startPacket = current_ts_packet;
				 for (__int64 i = startPacket-1; i >= 1; i--) {
					 current_ts_packet = i;
					 if (hexModeSwitch == 1)
					 {
						 if (binToHex(getBytes(12,24)) == pid) 
						 {
							 GetPacket(current_ts_packet);
							 break;
						 }
					 }
					 else 
					 {
						 if (binToDec(getBytes(12,24)) == pid) 
						 {
							 GetPacket(current_ts_packet);
							 break;
						 }
					 }
				 }
			 }

	private: void openAbtForm ()
			 {
				 AboutForm ^abtFrm = gcnew AboutForm;
				 abtFrm->ShowDialog();
			 }
	private: void reset () {
 				 textBox1->Text = "";
				 textBox2->Text = "";
				 textBox3->Text = "";
				 groupBox2->Text = "TS packet";
				 textBox5->Text = "";
				 textBox6->Text = "";
				 textBox7->Text = "";
				 textBox8->Text = "";
				 textBox9->Text = "";
				 textBox10->Text = "";
				 textBox11->Text = "";
				 textBox12->Text = "";


				 end_of_file = 0;
				 ip_file_length = 0;
				 current_ts_packet = -1;
				 __int64 packetsNumb = 0;
				 __int64 packetStart = 0;
				 ip_filepath = "";
			 }
	private: void hexMode (bool enableHex) {				 
				 if (enableHex == true)
					 hexModeSwitch = 1;
				 else 
					 hexModeSwitch = 0;
				 if (current_ts_packet != -1)
					 GetTSheader();
				 if ((textBox19->Enabled == true) & (textBox19->Text != ""))
					 textBox19->Text = textBox9->Text;
			 }
	private: void listAllPids() {
				 __int64 rememberPacketsState = current_ts_packet;
				 int rememberProgressState = progressBar1->Value;
				 int pids = 0;
				 bool pidFound;
				 array<String^, 2>^ pidList = gcnew array<String^, 2>(100,2);
					 //System::Diagnostics::Stopwatch ^mySW = gcnew System::Diagnostics::Stopwatch;
					 //mySW->Start();				 
				 for (__int64 i = 1; i <= packetsNumb; i++) {
					 current_ts_packet = i;
					 if (i%50 == 0)
						 progressBar1->Value = Convert::ToInt16(10000*current_ts_packet/packetsNumb);
					 pidFound = false;
					 for (int arrEnum = 0; arrEnum < pids; arrEnum++)
					 {
						 if (pidList[arrEnum,0] == getBytes(12,24))
						 {
							 pidFound = true;
							 pidList[arrEnum,1] = (Convert::ToInt64(pidList[arrEnum,1])+1).ToString();
							 break;
						 }
					 }
					 if (pidFound == false)
					 {
						 pidList[pids,0] = getBytes(12,24);
						 pidList[pids,1] = (Convert::ToInt64(pidList[pids,1])+1).ToString();
						 pids ++;
					 }
				 }				 
				 for (int i=0; i<pids; i++){
					 if (hexModeSwitch == 1)
						 pidList[i,0] = binToHex(pidList[i,0]);
					 else
						 pidList[i,0] = binToDec(pidList[i,0]);
				 }
				 current_ts_packet = rememberPacketsState;
				 progressBar1->Value = rememberProgressState;
				 progressBar1->Value = Convert::ToInt16(10000*current_ts_packet/packetsNumb);
					 //mySW->Stop();
					 //MessageBox::Show(mySW->ElapsedMilliseconds.ToString() + " ms");
				 PidList ^pl = gcnew PidList;				 
				 pl->setTable(pidList,pids);
				 pl->ShowDialog();
			 }
	private: void separator() {
				 __int64 savePacketState = current_ts_packet;
				 __int64 startPacket = 1;
				 for (__int64 i = startPacket+1; i <= packetsNumb; i++) {
					 current_ts_packet = i;
					 if (binToHex(getBytes(12,24)) == "0x0000") 
					 {
						 String^ k = getBytes(1,188);						 
						 break;
					 }
				 }
				 current_ts_packet = savePacketState;
				 
			 }
};
}
